<?php
namespace Home\Controller;
use \Home\Model\IndexModel;
use \Home\Controller\SafeController;
final class IndexController{
	//
	public $uid;
	public $name;
	public $qq;
	public $phone;
	
	//分页的参数
	public $pagesize;		//每页行数
	public $page;			//当前页数
	public $startrows;		//开始行号
	
	//对象
	public $indexModelObj;
	public $safeObj;
	
	public function __construct(){
		session_start();

		if(isset($_SESSION['uid'])){
		$this->uid = $_SESSION['uid'];
		$this->qq = $_SESSION['qq'];
		$this->name = $_SESSION['name'];
		$this->phone = $_SESSION['phone'];
		}
		$this->pagesize = $GLOBALS['config']['pagesize'];		//每页行数
		$this->page = isset($_GET['page'])?$_GET['page']:1;		//当前页数
		if($_GET['page']==null){
            $this->page = 1;
        }
		$this->startrows = ($this->page-1)*$this->pagesize;					//开始行号
		$this->indexModelObj = new IndexModel();
		$this->safeObj = new SafeController();
	}
	
    public function index(){
        // echo "<script src="."https:\/\/cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"."></script>
        // <script src="."loginComponent.vue"."></script>
        // <script src="."app.js"."></script>
        // <script src="."./app/js/app.8bd58442.js"."></script>
        
        // <script src="."./app/js/About.ef47c3de.js"."></script>
        // <script src="."./app/js/My.99cf0ce0.js"."></script>
        // ";
        //<script src="."./app/js/chunk-vendors.04f06154.js"."></script>
        include(VIEW_PATH."index.html");
        //重定向浏览器 
        //header("Location: http://book.wowow.top/app/index.html"); 
    }
	
	//关于我们
	public function about(){
		include VIEW_PATH."about.html";
	}
	
	//活动公示
	public function activity(){
		include VIEW_PATH."activity.html";
	}
	
	//活动信息获取
	public function getActivity(){
		//获取总记录数(前台只展示已结束的活动)
		$rows = $this->indexModelObj->getActivityRows();
		//分页
		include "./Common/page.php";
		
		//获取某用户的捐款记录结果集
		$arrs = $this->indexModelObj->getActivity($this->startrows,$this->pagesize);
		//遍历$arrs，通过每条记录的id获取图片结果集
		$picArrs = [];
		foreach($arrs as $arr){
			$id = $arr['activity_id'];
			$picArr = $this->indexModelObj->getActivityPic($id);
			array_push($picArrs,$picArr);
		}
		//下面这一段，花了一个下午，把数组进行了一个重组
		//比较坑的是，foreach的遍历是对拷贝数组...必须靠&来使用引用才能使得对数组的修改成功
		foreach ($picArrs as $k=>$v){
		      foreach ($v as $v1){
		          foreach($arrs as &$arr){
		            if($v1['activity_id']==$arr['activity_id']){
		              	$arr['pic'] = $v;
		                break;
		            }
		          }
                break;
		      }
		}
		//构造一个结果数组
		//包含请求状态、页数信息、结果集等
		$res = [
			'code' => 200,
			'page' => $this->page,
			'start' => $start,
			'end' => $end,
			'act_arr' => $arrs
		];
		echo json_encode($res);
	}
}
?>